package com.hualan.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.CourseClassifyMapper;
import com.hualan.sfzy.mapper.CourseMapper;
import com.hualan.sfzy.model.entity.CourseClassify;
import com.hualan.sfzy.service.CourseClassifyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.*;

@Service
public class CourseClassifyServiceImpl implements CourseClassifyService {
    @Autowired
    private CourseClassifyMapper courseClassifyMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public int add(CourseClassify entity) {

        return courseClassifyMapper.insert(entity);
    }

    @Override
    public int edit(CourseClassify entity) {
        return courseClassifyMapper.update(entity);
    }

    @Override
    public int remove(Integer id) {
        Integer[] ids = new Integer[]{id};
        int result = courseMapper.selectCountByCoursetype(ids);
        if (result > 0) {
            throw  new RuntimeException("有课程属于该分类，不能被删除");
        }

         return courseClassifyMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        System.out.println(Arrays.toString(ids));
        int result = courseMapper.selectCountByCoursetype(ids);
        System.out.println(result);
        if (result > 0) {
            throw  new RuntimeException("有课程属于该分类，不能被删除");
        }

        return courseClassifyMapper.deleteBatch(ids);
    }

    @Override
    public List<CourseClassify> find() {
        List<CourseClassify> courseClassifyList=courseClassifyMapper.select(new CourseClassify());

        return courseClassifyList;
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, CourseClassify entity) {
        PageHelper.startPage(pageNum, pageSize);

        List<CourseClassify> courseClassifyList=courseClassifyMapper.select(entity);
        PageInfo<CourseClassify> pageInfo = new PageInfo<>(courseClassifyList);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(CommonConstant.PAGE_LIST, courseClassifyList);    // 当前页数据列表
        resultMap.put(CommonConstant.TOTAL, pageInfo.getTotal());      // 总记录数
        resultMap.put(CommonConstant.PAGE_NUM, pageInfo.getPageNum()); // 当前页码
        resultMap.put(CommonConstant.PAGE_SIZE, pageInfo.getPageSize()); // 每页大小
        resultMap.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());  // 总页数

        return resultMap;


    }

    @Override
    public CourseClassify findById(Integer id) {
        return courseClassifyMapper.selectById(id);
    }
}
